package Zoo.view.leaderview;

import Zoo.entity.LeaderEntity;
import Zoo.handler.LeaderView1Handler;
import Zoo.impl.LeaderService1Impl;
import Zoo.service.LeaderService1;
import Zoo.util.TableDTO;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.Vector;

/**
 * 1、园长的动物饲养员信息主界面
 */
public class LeaderView1 extends JFrame {
    JPanel northPanel = new JPanel();
    JButton addBtn = new JButton("增加");
    JButton updateBtn = new JButton("修改");
    JButton delBtn = new JButton("删除");
    JTextField searchTxt = new JTextField(15);
    JButton searchBtn = new JButton("查询");


    LeaderView1Handler leaderView1Handler;

    public LeaderView1() {
        super("动物园园长界面");
        Container contentPane = getContentPane();
        leaderView1Handler = new LeaderView1Handler(this);

        //放置北边的组件
        layoutNorth(contentPane);


        Vector<Vector<Object>> data = getVectors();


        //tablemodel
        LeaderTableModel1 leaderTableModel1 = LeaderTableModel1.assembleModel(data);
        //jtable
        JTable jTable = new JTable(leaderTableModel1);
        //设置表头
        JTableHeader tableHeader = jTable.getTableHeader();
        tableHeader.setFont(new Font(null, Font.BOLD, 16));
        tableHeader.setForeground(Color.RED);

        //设置表格体
        jTable.setFont(new Font(null, Font.PLAIN, 14));
        jTable.setForeground(Color.black);
        jTable.setGridColor(Color.BLACK);
        jTable.setRowHeight(30);
        //设置多行选择
        jTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        //设置表格列的渲染方式
        Vector<String> columns = leaderTableModel1.getColumns();
        LeaderCellRender1 render1 = new LeaderCellRender1();
        for (int i = 0; i < columns.size(); i++) {
            TableColumn column = jTable.getColumn(columns.get(i));
            column.setCellRenderer(render1);
        }


        JScrollPane jScrollPane = new JScrollPane(jTable);
        contentPane.add(jScrollPane);

        setSize(600, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //setResizable(false);
        setVisible(true);
    }

    //查询功能
    private Vector<Vector<Object>> getVectors() {

        LeaderService1 leaderService1 = new LeaderService1Impl();
        LeaderEntity leaderEntity = new LeaderEntity();
        leaderEntity.setSearchKey(searchTxt.getText().trim());
        TableDTO tableDTO = leaderService1.retrieveLeader1(leaderEntity);
        Vector<Vector<Object>> data = tableDTO.getData();
        return data;
    }

    //放置北边组件
    private void layoutNorth(Container contentPane) {
        //增加事件监听
        addBtn.addActionListener(leaderView1Handler);
        updateBtn.addActionListener(leaderView1Handler);
        delBtn.addActionListener(leaderView1Handler);
        searchBtn.addActionListener(leaderView1Handler);

        northPanel.add(addBtn);
        northPanel.add(updateBtn);
        northPanel.add(delBtn);
        northPanel.add(searchTxt);
        northPanel.add(searchBtn);
        contentPane.add(northPanel, BorderLayout.NORTH);
    }


    //查询功能的实现
    public void reloadTable() {
        Vector<Vector<Object>> data = getVectors();
        LeaderTableModel1 leaderTableModel1 = new LeaderTableModel1();
        leaderTableModel1.updateModel(data);

    }


}

class LeaderCellRender1 extends DefaultTableCellRenderer {
    //在每一行的每一列显示之前都会调用
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if (row % 2 == 0) {
            setBackground(Color.LIGHT_GRAY);
        } else {
            setBackground(Color.WHITE);
        }
        setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
}

//自定义tablemodel
class LeaderTableModel1 extends DefaultTableModel {
    static Vector<String> columns = new Vector<>();
    private static LeaderTableModel1 leaderTableModel1 = new LeaderTableModel1();

    static {
        columns.addElement("动物饲养员id");
        columns.addElement("身份证号");
        columns.addElement("联系电话");
        columns.addElement("动物饲养员姓名");

    }

    public LeaderTableModel1() {
        super(null, columns);
    }

    public static LeaderTableModel1 assembleModel(Vector<Vector<Object>> data) {
        leaderTableModel1.setDataVector(data, columns);
        return leaderTableModel1;
    }


    public static void updateModel(Vector<Vector<Object>> data) {
        leaderTableModel1.setDataVector(data, columns);
    }

    public static Vector<String> getColumns() {
        return columns;
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }


}